﻿using System;
using System.Collections;
using System.Text;
using UnityEngine;
using yoyo;

public class Res
{
    public static AssetBundle abData = null;

    public static void ReadAB(string path ,Action<AssetBundle> act )
    {
        NetInstrument.AssetBundle(path, act);
    }

    /// <summary>
    /// 读取Texture2D
    /// </summary>
    /// <param name="path">路径</param>
    /// <param name="act">回调</param>
    /// <param name="type">下载类型</param>
    /// <returns></returns>
    public static Texture2D ReadTexture2D(string path , Action<Texture2D> act,ResLoadType type =ResLoadType.AB)
    {
        Texture2D tex = null;
        switch (type)
        {
            case ResLoadType.Res:
                tex =Resources.Load<Texture2D>(path);
                break;
            case ResLoadType.Path:
                {
                    NetInstrument.Texture2D(path, (texture) =>
                    {
                        act(texture);
                    });
                }
                break;
            case ResLoadType.AB:
                {
                    tex=abData.LoadAsset<Texture2D>(path);
                }
                break;

        }
        return tex;
    }


    /// <summary>
    /// 读取string
    /// </summary>
    /// <param name="path">路径</param>
    /// <param name="act">回调</param>
    /// <param name="type">下载类型</param>
    /// <returns></returns>
    public static string ReadText(string path  ,Action<string> act , ResLoadType type = ResLoadType.AB)
    {
        string str =null;
        switch (type)
        {
            case ResLoadType.Res:
                str = Resources.Load<TextAsset>(path).text;
                break;
            case ResLoadType.Path:
                {
                    NetInstrument.Get(path, (www) =>
                    {
                        str = www.downloadHandler.text;
                        act(str);
                    });
                }
                break;
            case ResLoadType.AB:
                {
                    str = abData.LoadAsset<TextAsset>(path).text;
                }
                break;

        }
        return str;
    }


    /// <summary>
    /// 读取Byte
    /// </summary>
    /// <param name="path">路径</param>
    /// <param name="act">回调</param>
    /// <param name="type">下载类型</param>
    /// <returns></returns>
    public static byte[] ReadByte(string path, Action<byte[]> act,ResLoadType type = ResLoadType.AB)
    {
        byte []  by = null;
        switch (type)
        {
            case ResLoadType.Res:
                by = Resources.Load<TextAsset>(path).bytes;
                break;
            case ResLoadType.Path:
                {
                    NetInstrument.Get(path, (www) =>
                    {
                        by = www.downloadHandler.data;
                        act(by);
                    });
                }
                break;
            case ResLoadType.AB:
                {
                    by = abData.LoadAsset<TextAsset>(path).bytes;
                }
                break;

        }
        return by;
    }




    public static void UnLoad(AssetBundle ab)
    {
        ab.Unload(false);
        Resources.UnloadUnusedAssets();
    }


    //public static byte[] ReadBytes(string url)
    //{
    //    if (!GlobalFile.ab)
    //    {
    //        return Resources.Load<TextAsset>(url).bytes;
    //    }
    //    else
    //    {
    //        string[] str = url.Split('/');
    //        url = str[str.Length - 1];
    //        return abData.LoadAsset<TextAsset>(url).bytes;
    //    }
    //}

    
}
